const fp = require("lodash/fp");
const _ = require("lodash");

//1. 获取最后一个元素的in_stock
let isLastInStock = fp.flowRight(_.curry(fp.prop)("in_stock"), fp.last);

//2. 获取第一个元素的name
let getFirstName = fp.flowRight(_.curry(fp.prop)("name"), fp.first);

let _average = function (xs) {
    return fp.reduce(fp.add, 0, xs) / xs.length;
};

//3.改写函数
let averageDollarValue = fp.flowRight(
    _average,
    fp.map(_.curry(fp.prop)("dollar_value"))
);

//4.实现函数
let _underscore = fp.replace(/\W+/g, "_");
let sanitizeNames = fp.map(fp.flowRight(_.toLower, _underscore));

module.exports = {
    isLastInStock,
    getFirstName,
    averageDollarValue,
    sanitizeNames,
};
